package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.UserParkingGuidance;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.service.UserParkingGuidanceService;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户观看停车引导记录
 *
 * @author: ShiGuang
 * @create: 2021-12-16 15:54
 * @description:
 */
@RestController
@RequestMapping("/UserParkingGuidance")
public class UserParkingGuidanceController {

    @Autowired
    private UserParkingGuidanceService userParkingGuidanceService;

    @Autowired
    private TokenService tokenService;

    /**
     * 判断用户是否看过新的停车规范，没有则展
     *
     * @param token
     * @param lo       经度
     * @param la       纬度
     * @param mapType  地图类型
     * @param request
     * @param response
     */
    @RequestMapping("/getInfo")
    public void getInfo(String token, Double lo, Double la, Integer mapType, HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<>();

        try {
            User user = tokenService.getUser(token);
            Integer userId = user.getUserId();

            // 如果地图类型为空，默认谷歌
            if (mapType == null) {
                mapType = SysConstant.GOOGLE;
            }

            Map<String, Object> params = userParkingGuidanceService.getParkingGuidance(userId, lo, la, mapType);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, params);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }


    /**
     * 添加用户观看引导记录
     *
     * @param token       令牌
     * @param accountId   帐户id
     * @param featureType 功能类型
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/saveInfo")
    public void saveInfo(String token, Integer accountId, Integer featureType, HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<>();

        try {
            if (accountId != null && featureType != null) {
                User user = tokenService.getUser(token);
                UserParkingGuidance userParkingGuidance = new UserParkingGuidance();
                userParkingGuidance.setUserId(user.getUserId());
                userParkingGuidance.setAccountId(accountId);
                userParkingGuidance.setFeatureType(featureType);
                userParkingGuidanceService.addUserParkingGuidance(userParkingGuidance);
                map.put(ResponseCode.RET, ResponseCode.SUCC);
            }
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }
}
